<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use library\Controller;
use library\tools\Data;
use think\Db;
use app\store\model\Common;
/**
 * 系统图片管理
 * Class Documentlist
 * @package app\store\controller
 */
class Banner extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'SystemBanner';
    protected $table1 = 'system_banner';

    /**
     * 图片管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '图片管理';
        $query = $this->_query($this->table)->equal('status,cate_id')->like('title');
        $query->where(['is_deleted' => '0'])->order('sort desc,id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        $this->clist = $this->banner_cate();
        foreach($data as $k =>&$v){
            $v['cate_name'] = $this->cate_name($v['cate_id']);
        }
    }

    private function banner_cate(){
        return Db::query("select * from system_banner_cate");
    }

    private function cate_name($id){
        return Db::table("system_banner_cate")->where(["id"=>$id])->value('title');
    }


    public function shua(){
        $list = Db::query("select id,logo from $this->table1");
        $common = new Common();
        $common->shua($list,$this->table1,1);

        $url = url('@admin') . '#' . url('@admin/banner/index') . '?spm=' . $this->request->get('spm');
        $this->success('刷新成功！', $url);
    }

    /**
     * 添加图片
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加图片';
        $this->isAddMode = '1';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑图片
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑图片';
        $this->isAddMode = '0';
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _form_filter(&$data)
    {
        // 生成ID
        if (empty($data['id'])) $data['id'] = Data::uniqidNumberCode(14);
        if ($this->request->isGet()) {
            $this->cates = $this->banner_cate();
        } elseif ($this->request->isPost()) {
            //if (empty($data['logo'])) $this->error('LOGO不能为空，请上传图片');
            //if (empty($data['image'])) $this->error('展示图片不能为空，请上传图片');
        }
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('编辑成功！', 'javascript:history.back()');
        }
    }

    /**
     * 启用图片
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 禁用图片
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }



    /**
     * 删除图片
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}
